<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="企业名称" prop="corpName">
        <el-input
          v-model="queryParams.corpName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一代码" prop="corpCredit">
        <el-input
          v-model="queryParams.corpCredit"
          placeholder="请输入统一代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入企业联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业联系方式" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入企业联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="走访时间" prop="visitDate">
        <el-input
          v-model="queryParams.visitDate"
          placeholder="请输入走访时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="走访情况" prop="visitRemark">
        <el-input
          v-model="queryParams.visitRemark"
          placeholder="请输入走访情况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="走访状态" prop="visitStatus" label-width="300">
        <el-select
          clearable
          v-model="queryParams.visitStatus"
          placeholder="请选择走访状态"
        >
          <el-option
            v-for="item in visitStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="问卷状态"
        prop="questionnaireStatus"
        label-width="300"
      >
        <el-select
          clearable
          v-model="queryParams.questionnaireStatus"
          placeholder="请选择问卷状态"
        >
          <el-option
            v-for="item in questionnaireStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:visitUser:export']"
        >导出走访问卷
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleCorpExport"
          v-hasPermi="['system:visitUser:export']"
        >导出企业问卷
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="visitUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业名称" align="center" prop="corpName"/>
      <el-table-column label="统一代码" align="center" prop="corpCredit"/>
      <el-table-column label="客户经理" align="center" prop="managerName"/>
      <el-table-column label="客户经理所在机构" align="center" prop="agencyName"/>
      <el-table-column label="企业联系人" align="center" prop="contactPerson"/>
      <el-table-column label="企业联系方式" align="center" prop="contactPhone"/>
      <el-table-column label="走访时间" align="center" prop="visitDate"/>
      <el-table-column label="走访情况" align="center" prop="visitRemark"/>
      <el-table-column label="走访状态" align="center" prop="visitStatus"/>
      <el-table-column
        label="问卷状态"
        align="center"
        prop="questionnaireStatus"
      />
      <el-table-column label="授信金额(万元)" align="center" prop="creditAmount"/>
      <el-table-column label="用信金额(万元)" align="center" prop="useAmount"/>
      <!--      <el-table-column label="企业简称" align="center" prop="shortName" />-->
      <!--      <el-table-column label="企业经营地址是否与注册地址一致" align="center" prop="addressIsSyn" />-->
      <!--      <el-table-column label="地址是否一致的说明" align="center" prop="isSynComment" />-->
      <el-table-column label="打卡地址" align="center" prop="corpLocation"/>
      <!--      <el-table-column label="企业实际地址" align="center" prop="addr" />-->
      <!--      <el-table-column label="是否已填写问卷 是-1  否-0" align="center" prop="qrcode" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            :disabled="scope.row.answer ? false : true"
            v-hasPermi="['system:visitUser:remove']"
            @click="checkzf(scope.row)"
          >走访问卷
          </el-button>
          <el-button
            size="mini"
            type="text"

            :disabled="scope.row.qsInfo ? false : true"
            v-hasPermi="['system:visitUser:edit']"
            @click="checkqy(scope.row)"
          >企业问卷
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改银企对接-走访企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="企业名称" prop="corpName">
          <el-input v-model="form.corpName" placeholder="请输入企业名称"/>
        </el-form-item>
        <el-form-item label="统一代码" prop="corpCredit">
          <el-input v-model="form.corpCredit" placeholder="请输入统一代码"/>
        </el-form-item>
        <el-form-item label="企业联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入企业联系人"/>
        </el-form-item>
        <el-form-item label="企业联系方式" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入企业联系方式"/>
        </el-form-item>
        <el-form-item label="走访时间" prop="visitDate">
          <el-input v-model="form.visitDate" placeholder="请输入走访时间"/>
        </el-form-item>
        <el-form-item label="走访情况" prop="visitRemark">
          <el-input v-model="form.visitRemark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="走访图片地址" prop="visitPictureUrl">
          <el-input v-model="form.visitPictureUrl" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="问卷答案" prop="answer">
          <el-input v-model="form.answer" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="授信金额(万元)" prop="creditAmount">
          <el-input v-model="form.creditAmount" placeholder="请输入授信金额(万元)"/>
        </el-form-item>
        <el-form-item label="用信金额(万元)" prop="useAmount">
          <el-input v-model="form.useAmount" placeholder="请输入用信金额(万元)"/>
        </el-form-item>
        <el-form-item label="推送批次号" prop="pushId">
          <el-input v-model="form.pushId" placeholder="请输入推送批次号"/>
        </el-form-item>
        <el-form-item label="企业简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入企业简称"/>
        </el-form-item>
        <el-form-item label="企业经营地址是否与注册地址一致" prop="addressIsSyn">
          <el-input v-model="form.addressIsSyn" placeholder="请输入企业经营地址是否与注册地址一致"/>
        </el-form-item>
        <el-form-item label="地址是否一致的说明" prop="isSynComment">
          <el-input v-model="form.isSynComment" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="打卡地址" prop="corpLocation">
          <el-input v-model="form.corpLocation" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="企业实际经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入企业实际经度"/>
        </el-form-item>
        <el-form-item label="企业实际纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入企业实际纬度"/>
        </el-form-item>
        <el-form-item label="企业实际地址" prop="addr">
          <el-input v-model="form.addr" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="是否已填写问卷 是-1  否-0" prop="qrcode">
          <el-input v-model="form.qrcode" placeholder="请输入是否已填写问卷 是-1  否-0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <zfquestionnaireDetail ref="zfquestionnaireDetail"></zfquestionnaireDetail>
    <qyquestionnaireDetail ref="qyquestionnaireDetail"></qyquestionnaireDetail>
  </div>
</template>

<script>
import {
  addVisitUser,
  delVisitUser,
  getVisitUser,
  listVisitUser,
  updateVisitUser
} from "@/api/business/visituser/visitUser";
import qyquestionnaireDetail from "./module/qyquestionnaireDetail.vue";
import zfquestionnaireDetail from "./module/zfquestionnaireDetail.vue";

export default {
  name: "VisitUser",
  components: {
    qyquestionnaireDetail,
    zfquestionnaireDetail,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 银企对接-走访企业表格数据
      visitUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        corpName: null,
        corpCredit: null,
        contactPerson: null,
        contactPhone: null,
        visitDate: null,
        visitRemark: null,
        visitStatus: null,
        answer: null,
        creditAmount: null,
        useAmount: null,
        pushId: null,
        shortName: null,
        addressIsSyn: null,
        isSynComment: null,
        corpLocation: null,
        longitude: null,
        latitude: null,
        addr: null,
        qrcode: null,
        selectedRows: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      questionnaireStatusList: [
        {
          value: "问卷已进行",
          label: "问卷已进行",
        },
        {
          value: "问卷未进行",
          label: "问卷未进行",
        },
      ],
      visitStatusList: [
        {
          value: "已走访无需求",
          label: "已走访无需求",
        },
        {
          value: "已走访有意向",
          label: "已走访有意向",
        },
        {
          value: "已走访且授信",
          label: "已走访且授信",
        },
        {
          value: "有意向末准入",
          label: "有意向末准入",
        },
        {
          value: "已授信且用信",
          label: "已授信且用信",
        },
        {
          value: "未走访",
          label: "未走访",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询银企对接-走访企业列表 */
    getList() {
      this.loading = true;
      listVisitUser(this.queryParams).then(response => {
        this.visitUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        corpName: null,
        corpCredit: null,
        contactPerson: null,
        contactPhone: null,
        visitDate: null,
        visitRemark: null,
        visitStatus: null,
        questionnaireStatus: null,
        visitPictureUrl: null,
        answer: null,
        creditAmount: null,
        useAmount: null,
        pushId: null,
        shortName: null,
        addressIsSyn: null,
        isSynComment: null,
        corpLocation: null,
        longitude: null,
        latitude: null,
        addr: null,
        qrcode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {

      let selectedRows = []
      let item = {}
      for (let i = 0; i < selection.length; i++) {
        item.managerName = selection[i].managerName
        item.corpCredit = selection[i].corpCredit
        item.corpName = selection[i].corpName
        item.agencyName = selection[i].agencyName
        item.visitDate = selection[i].visitDate

        item.answerVo = selection[i].answer
        item.qsInfoVo = selection[i].qsInfo
        selectedRows.push(item)
      }
      let str = JSON.stringify(selectedRows)
      this.queryParams.selectedRows = str
      // this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加银企对接-走访企业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getVisitUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改银企对接-走访企业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateVisitUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVisitUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除银企对接-走访企业编号为"' + userIds + '"的数据项？').then(function () {
        return delVisitUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      debugger;
      this.queryParams.exportType = '1'
      this.download('visit/visitUser/export', {
        ...this.queryParams
      }, `走访问卷_${new Date().getTime()}.xlsx`)
    },
    handleCorpExport() {
      debugger;
      this.queryParams.exportType = '2'
      this.download('visit/visitUser/export', {
        ...this.queryParams
      }, `企业问卷_${new Date().getTime()}.xlsx`)
    },
    // 查看走访问卷
    checkzf(val) {
      debugger;
      console.log(JSON.parse(val.answer), '查看内容')
      this.$refs.zfquestionnaireDetail.isShow(val);
    },
    checkqy(val) {
      console.log(JSON.parse(val.qsInfo), '查看内容')
      this.$refs.qyquestionnaireDetail.isShow(val);
    },







  }
};
</script>
